package com.topway.dsmflex.dao.synchro;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.topway.dsmflex.dao.BaseDao;
import com.topway.dsmflex.model.synchro.DataColumnBean;
import com.topway.dsmflex.model.synchro.DataSourceBean;
import com.topway.dsmflex.model.synchro.DataTableBean;


/**
 * 
 * @author liusy 2013-05-2
 *
 */
public class TableDao extends BaseDao{
	
	private static Logger log = Logger.getLogger(TableDao.class);
	
	private static TableDao tableDao = new TableDao();
	

	protected String [] sourceColumn = new String[]{};
	
	
	public TableDao()
	{
		
	}
	
	public synchronized static TableDao getTableDao()
	{
		if(null==tableDao)
		{
			tableDao = new TableDao();
		}
		return tableDao;
	}
	
	public List<DataColumnBean> getColumnInfo(DataSourceBean  _daSource,String tablename) throws SQLException
	{
		this.getConnection(_daSource);
		List<DataColumnBean> list = new ArrayList<DataColumnBean>();
		List<String> listStr = new ArrayList<String>();
		listStr.add("");
		try {
			DatabaseMetaData dbMetaData = conn.getMetaData();
			ResultSet colRet = dbMetaData.getColumns(null,null, tablename,"%");
			DataColumnBean dc = null;
			while(colRet.next()) {
				dc = new DataColumnBean(colRet.getString("COLUMN_NAME").toUpperCase());
				listStr.add(dc.getColumnNameSource().toUpperCase());
				list.add(dc);
			}
			sourceColumn = new String[list.size()+1];
			listStr.toArray(sourceColumn);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return list;
	
	}
	
	/**
	 * 先运行getColumnInfo,才有值
	 * @return
	 */
	public String [] getSourceColumn()
	{
		return this.sourceColumn;
	}
	
	
	public List<DataTableBean> getAllDataTable(DataSourceBean  _daSource) {
		this.getConnection(_daSource);
		List<DataTableBean> tables = new ArrayList<DataTableBean>();
		try {
			DatabaseMetaData dbMetaData = this.conn.getMetaData();
			//"TABLE", "VIEW", "SYSTEM   TABLE", "GLOBAL   TEMPORARY", "LOCAL   TEMPORARY", "ALIAS", "SYNONYM"
			String[] types = { "TABLE","VIEW" };
			//ResultSet tabs = dbMetaData.getTables(null,_daSource.getUserName(), null, types);
			ResultSet tabs = dbMetaData.getTables(null,null, null, types);
			DataTableBean dt;
			while (tabs.next()) {
				if(((String)tabs.getObject("TABLE_NAME")).indexOf("$")!=-1)
					continue;
				dt = new DataTableBean(tabs.getObject("TABLE_NAME").toString().toUpperCase());
				dt.setSourceDataSource(this.ds);
				dt.setDsSource(this.ds.getDataSourceName());
				dt.setTargetDataSource(this.ds);
				dt.setDsTarget(this.ds.getDataSourceName());
				tables.add(dt);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return tables;

	}

	public String [] getAllDataTableArray(DataSourceBean  _daTarget) {
		this.getConnection(_daTarget);
		List<String> tables = new ArrayList<String>();
		try {
			DatabaseMetaData dbMetaData = conn.getMetaData();
			//"TABLE", "VIEW", "SYSTEM   TABLE", "GLOBAL   TEMPORARY", "LOCAL   TEMPORARY", "ALIAS", "SYNONYM"
			String[] types = { "TABLE" };
			//ResultSet tabs = dbMetaData.getTables(null,_daTarget.getUserName(), null, types);
			ResultSet tabs = dbMetaData.getTables(null,null, null, types);
			int i=0;
			while (tabs.next()) {
				if(((String)tabs.getObject("TABLE_NAME")).indexOf("$")!=-1)
					continue;
				tables.add(tabs.getObject("TABLE_NAME").toString().toUpperCase());
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		String[] tb= new String[tables.size()];
		tables.toArray(tb);
		return tb; 

	}

	public String [] getColumnInfoArray(DataSourceBean  _daTarget,String tablename)
	{
		this.getConnection(_daTarget);
		List<String> list = new ArrayList<String>();
		list.add("");
		try {
			DatabaseMetaData dbMetaData = conn.getMetaData();
			ResultSet colRet = dbMetaData.getColumns(null,null, tablename,"%");
			while(colRet.next()) {
				list.add(colRet.getString("COLUMN_NAME").toUpperCase());
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		String[] targetColumn = new String[list.size()];
		list.toArray(targetColumn);
		return targetColumn;
	}
	
	
	public static void main(String[] args) {
		String [] tables = new String[10];
		int i = 0;
		while(i<10)
		{
			tables[++i]="df";
		}
	}
}
